package cn.com.pc.content.dto;

import cn.com.pc.aaa.model.Identity;
import cn.com.pc.content.domain.Site;
import cn.com.pc.content.domain.authority.SiteOperator;
import cn.com.pc.content.util.BeanUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.Min;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor
public class SiteOperatorDTO {
    private Long id;
    @Min(value = 1, message = "操作员id（operatorId) 必须大于 0")
    private Long operatorId;
    private String operatorName;
    private String personName;
    private Site site;
    @JsonFormat(timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime createdAt;
    private Long createdUserId;
    private String createdUserName;
    private String createdPersonName;


    public static SiteOperatorDTO from(SiteOperator siteOperator) {
        SiteOperatorDTO siteOperatorDTO = new SiteOperatorDTO();
        BeanUtils.copyProperties(siteOperator, siteOperatorDTO);
        return siteOperatorDTO.fillUsers();
    }

    private SiteOperatorDTO fillUsers() {
        Identity operator = Identity.of(operatorId);
        Identity createdUser = Identity.of(createdUserId);

        operatorName = operator.getName();
        personName = operator.getPersonName();
        createdUserName = createdUser.getName();
        createdPersonName = createdUser.getPersonName();

        return this;
    }

    public SiteOperator build(Site site, Long createdUserId) {
        return SiteOperator.builder()
                .operatorId(operatorId)
                .site(site)
                .createdUserId(createdUserId)
                .createdAt(LocalDateTime.now())
                .build();
    }
}
